﻿// -----------------------------------------------------------------------
// <copyright file="SaveLoadGameManager.cs" company="x">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace BeatEmUp
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Xml.Serialization;
    using Microsoft.Xna.Framework.Input;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class SaveLoadGameManager
    {
        /// <summary>
        /// Keyboard states
        /// </summary>
        private static KeyboardState keyboardPrevious, currentState;
        
        /// <summary>
        /// Information to save
        /// </summary>
        private static GameInformation gameInfo; 

        /// <summary>
        /// Current GameManager
        /// </summary>
        private static GameManager gameManager;

        /// <summary>
        /// It is true when loading game operation is in progress.
        /// </summary>
        public static bool isLoadingInProgress;

        /// <summary>
        /// Initializes save load manager.
        /// </summary>
        /// <param name="gameManager">Current game manager.</param>
        public static void SaveLoadManagerInitialize(GameManager gameManager)
        {
            SaveLoadGameManager.gameManager = gameManager;
        }

        /// <summary>
        /// Update method for GameManager.Update() usage.
        /// </summary>
        /// <param name="newGameInfo">Information updated in every update loop (current info).</param>
        /// <param name="loadedGameInfo">Information about loaded game that will overwrite current info.</param>
        internal static void Update(ref GameInformation newGameInfo, ref GameInformation loadedGameInfo)
        {
            SaveLoadGameManager.currentState = Keyboard.GetState();

            if (SaveLoadGameManager.currentState.IsKeyDown(Keys.F5) && SaveLoadGameManager.currentState != SaveLoadGameManager.keyboardPrevious)
            {
                SaveLoadGameManager.UpdateGameInformation(newGameInfo);
                SaveLoadGameManager.SaveGame("savedGame.dat", SaveLoadGameManager.gameInfo);
            }

            if (SaveLoadGameManager.currentState.IsKeyDown(Keys.F9) && SaveLoadGameManager.currentState != SaveLoadGameManager.keyboardPrevious)
            {
                loadedGameInfo = SaveLoadGameManager.LoadGame();
            }
            else
            {
                loadedGameInfo = null;
            }

            SaveLoadGameManager.keyboardPrevious = SaveLoadGameManager.currentState;
        }

        /// <summary>
        /// Updating current info.
        /// </summary>
        /// <param name="newGameInfo">Current info.</param>
        private static void UpdateGameInformation(GameInformation newGameInfo)
        {
            SaveLoadGameManager.gameInfo = newGameInfo;
        }

        /// <summary>
        /// Load game from file.
        /// </summary>
        /// <returns>Returns null if did not found save file or returns loaded from file GameInfo.</returns>
        private static GameInformation LoadGame()
        {
            if (File.Exists("savedGame.dat"))
            {
                isLoadingInProgress = true;
                string filename = "savedGame.dat";
                GameInformation gameInfo;
                Stream stream = File.Open(filename, FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                gameInfo = (GameInformation)formatter.Deserialize(stream);
                stream.Close();
                return gameInfo;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Saves current game info to a file.
        /// </summary>
        /// <param name="fileName">Given filename to save game in.</param>
        /// <param name="gameInfo">Game info to save.</param>
        private static void SaveGame(string fileName, GameInformation gameInfo)
        {
            Stream stream = File.Open(fileName, FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, gameInfo);
            stream.Close();
        }
    }
}
